<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="FBillNo">
        <el-input
          v-model="queryParams.FBillNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wm:warehousing:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wm:warehousing:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wm:warehousing:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wm:warehousing:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehousingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购入库单" align="center" prop="purchaseCode" />
      <el-table-column label="单据编号" align="center" prop="fbillNo" />
      <el-table-column label="单据类型" align="center" prop="fbillTypeID" />
      <el-table-column label="入库日期" align="center" prop="fdate" />
<!--      <el-table-column label="收料组织" align="center" prop="FStockOrgId" />-->
<!--      <el-table-column label="采购组织" align="center" prop="FPurOrgId" />-->
      <el-table-column label="供应商" align="center" prop="fsupplierId" />
<!--      <el-table-column label="货主类型" align="center" prop="FOwnerTypeIdHead" />-->
<!--      <el-table-column label="货主" align="center" prop="FOwnerIdHead" />-->
      <el-table-column label="物料编码" align="center" prop="fmaterialId" />
      <el-table-column label="物料说明" align="center" prop="fmaterialDesc" />
<!--      <el-table-column label="库存单位" align="center" prop="FStockUnitID" />-->
<!--      <el-table-column label="计价单位" align="center" prop="FPriceUnitId" />-->
<!--      <el-table-column label="货主类型1" align="center" prop="FOWNERTYPEID" />-->
<!--      <el-table-column label="采购单位" align="center" prop="FRemainInStockUnitId" />-->
<!--      <el-table-column label="货主1" align="center" prop="FOWNERID" />-->
<!--      <el-table-column label="0-可用 1-禁用" align="center" prop="status" />-->
<!--      <el-table-column label="金蝶云备注" align="center" prop="jdyFnote" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wm:warehousing:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePurchaseDataBack(scope.row)"
            v-hasPermi="['wm:warehousing:edit']"
          >回写ERP</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wm:warehousing:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购入库单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购入库单code" prop="purchaseCode">
          <el-input v-model="form.purchaseCode" placeholder="请输入采购入库单code" />
        </el-form-item>
        <el-form-item label="单据编号" prop="FBillNo">
          <el-input v-model="form.FBillNo" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="单据类型" prop="FBillTypeID">
          <el-input v-model="form.FBillTypeID" placeholder="请输入单据类型" />
        </el-form-item>
        <el-form-item label="入库日期" prop="FDate">
          <el-input v-model="form.FDate" placeholder="请输入入库日期" />
        </el-form-item>
        <el-form-item label="收料组织" prop="FStockOrgId">
          <el-input v-model="form.FStockOrgId" placeholder="请输入收料组织" />
        </el-form-item>
        <el-form-item label="采购组织" prop="FPurOrgId">
          <el-input v-model="form.FPurOrgId" placeholder="请输入采购组织" />
        </el-form-item>
        <el-form-item label="供应商" prop="FSupplierId">
          <el-input v-model="form.FSupplierId" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="货主类型" prop="FOwnerTypeIdHead">
          <el-input v-model="form.FOwnerTypeIdHead" placeholder="请输入货主类型" />
        </el-form-item>
        <el-form-item label="货主" prop="FOwnerIdHead">
          <el-input v-model="form.FOwnerIdHead" placeholder="请输入货主" />
        </el-form-item>
        <el-form-item label="物料编码" prop="FMaterialId">
          <el-input v-model="form.FMaterialId" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料说明" prop="FMaterialDesc">
          <el-input v-model="form.FMaterialDesc" placeholder="请输入物料说明" />
        </el-form-item>
        <el-form-item label="库存单位" prop="FStockUnitID">
          <el-input v-model="form.FStockUnitID" placeholder="请输入库存单位" />
        </el-form-item>
        <el-form-item label="计价单位" prop="FPriceUnitId">
          <el-input v-model="form.FPriceUnitId" placeholder="请输入计价单位" />
        </el-form-item>
        <el-form-item label="货主类型1" prop="FOWNERTYPEID">
          <el-input v-model="form.FOWNERTYPEID" placeholder="请输入货主类型1" />
        </el-form-item>
        <el-form-item label="采购单位" prop="FRemainInStockUnitId">
          <el-input v-model="form.FRemainInStockUnitId" placeholder="请输入采购单位" />
        </el-form-item>
        <el-form-item label="货主1" prop="FOWNERID">
          <el-input v-model="form.FOWNERID" placeholder="请输入货主1" />
        </el-form-item>
        <el-form-item label="金蝶云备注" prop="jdyFnote">
          <el-input v-model="form.jdyFnote" placeholder="请输入金蝶云备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarehousing, getWarehousing, delWarehousing, addWarehousing, updateWarehousing ,PurchaseDataBack } from "@/api/mes/wm/purchasewarehousing";

export default {
  name: "Warehousing",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购入库单表格数据
      warehousingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        FBillNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购入库单列表 */
    getList() {
      this.loading = true;
      listWarehousing(this.queryParams).then(response => {
        this.warehousingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseId: null,
        purchaseCode: null,
        FBillNo: null,
        FBillTypeID: null,
        FDate: null,
        FStockOrgId: null,
        FPurOrgId: null,
        FSupplierId: null,
        FOwnerTypeIdHead: null,
        FOwnerIdHead: null,
        FMaterialId: null,
        FMaterialDesc: null,
        FStockUnitID: null,
        FPriceUnitId: null,
        FOWNERTYPEID: null,
        FRemainInStockUnitId: null,
        FOWNERID: null,
        status: null,
        jdyFnote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购入库单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseId = row.purchaseId || this.ids
      getWarehousing(purchaseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购入库单";
      });
    },
    handlePurchaseDataBack(row){
      this.reset();
      const purchaseId = row.purchaseId || this.ids
      getWarehousing(purchaseId).then(response => {
        this.form = response.data;
        PurchaseDataBack(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.purchaseId != null) {
            updateWarehousing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehousing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseIds = row.purchaseId || this.ids;
      this.$modal.confirm('是否确认删除采购入库单编号为"' + purchaseIds + '"的数据项？').then(function() {
        return delWarehousing(purchaseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wm/warehousing/export', {
        ...this.queryParams
      }, `warehousing_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
